package com.gooluke.common.exception;

import com.gooluke.common.enums.BasicError;

import java.util.HashMap;
import java.util.Map;

public class BizException extends RuntimeException {

    private final int code;
    private final String message;
    private final Map<String, Object> context;

    // 默认500
    public BizException(String message) {
        this(500, message, null);
    }

    public BizException(int code, String message) {
        this(code, message, null);
    }

    // 通过 BasicError 构造异常
    public BizException(BasicError basicError) {
        this(basicError.getCode(), basicError.getMessage(), null);
    }

    public BizException(int code, String message, Map<String, Object> context) {
        super(message);
        this.code = code;
        this.message = message;
        this.context = context != null ? context : new HashMap<>();
    }

    // 添加上下文信息（用于日志排查）
    public BizException withContext(String key, Object value) {
        this.context.put(key, value);
        return this;
    }


}